﻿<ContentControl x:Class="Sinacor.UI.HelpViewer.View.IndexPanel"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Controls="clr-namespace:Sinacor.Infra.UI.Controls.SmartClient;assembly=Sinacor.Infra.UI.Controls"
    xmlns:CC="clr-namespace:Sinacor.UI.HelpViewer.View"
    IsTabStop="False" Focusable="False">
    <ContentControl.Resources>

        <Style TargetType="{x:Type ScrollViewer}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ScrollViewer}">
                        <Grid x:Name="Grid" Background="{TemplateBinding Background}">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="*"/>
                                <RowDefinition Height="Auto"/>
                            </Grid.RowDefinitions>
                            <Rectangle Fill="Transparent" x:Name="Corner" Grid.Column="1" Grid.Row="1"/>
                            <ScrollContentPresenter Margin="{TemplateBinding Padding}" x:Name="PART_ScrollContentPresenter" Grid.Column="0" Grid.Row="0" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}" CanContentScroll="{TemplateBinding CanContentScroll}" CanHorizontallyScroll="False" CanVerticallyScroll="False"/>
                            <Controls:SinacorVerticalScrollBar Cursor="Arrow" x:Name="PART_VerticalScrollBar" Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}" ViewportSize="{TemplateBinding ViewportHeight}" Maximum="{TemplateBinding ScrollableHeight}" Minimum="0" Value="{Binding Path=VerticalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="1" Grid.Row="0" AutomationProperties.AutomationId="VerticalScrollBar"/>
                            <Controls:SinacorHorizontalScrollBar Cursor="Arrow" x:Name="PART_HorizontalScrollBar" Visibility="{TemplateBinding ComputedHorizontalScrollBarVisibility}" ViewportSize="{TemplateBinding ViewportWidth}" Maximum="{TemplateBinding ScrollableWidth}" Minimum="0" Value="{Binding Path=HorizontalOffset, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" Grid.Column="0" Grid.Row="1" AutomationProperties.AutomationId="HorizontalScrollBar"/>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Storyboard x:Key="ShowListBoxAnimation">
            <DoubleAnimation Storyboard.TargetName="IndexListBox" Storyboard.TargetProperty="Opacity" BeginTime="0:0:0" Duration="0:0:0.2" From="0" To="1" />
            <DoubleAnimation Storyboard.TargetName="IndexTreeView" Storyboard.TargetProperty="Opacity" BeginTime="0:0:0" Duration="0:0:0.2" From="1" To="0" />
        </Storyboard>

        <Storyboard x:Key="ShowTreeViewAnimation">
            <DoubleAnimation Storyboard.TargetName="IndexListBox" Storyboard.TargetProperty="Opacity" BeginTime="0:0:0" Duration="0:0:0.2" From="1" To="0" />
            <DoubleAnimation Storyboard.TargetName="IndexTreeView" Storyboard.TargetProperty="Opacity" BeginTime="0:0:0" Duration="0:0:0.2" From="0" To="1" />
        </Storyboard>

        <XmlDataProvider x:Key="NavigationMapTreeView" XPath="/HelpTOC/HelpTOCNode" />
        <XmlDataProvider x:Key="NavigationMapListBox" XPath="/HelpTOC/HelpTOCNode/HelpTOCNode" />

        <!-- Style for ListBox -->

        <!-- Child ListBox -->

        <Style x:Key="ChildItemContainerStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Padding" Value="0" />
            <Setter Property="Margin" Value="0,0,0,0" />
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border Name="Border" SnapsToDevicePixels="True">
                            <Grid>
                                <TextBlock Text="{Binding XPath=@Title}" ToolTip="{Binding XPath=@Title}" TextTrimming="WordEllipsis" Style="{DynamicResource ControlFont}" />
                            </Grid>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="True">
                                <Setter Property="Panel.Background" TargetName="Border" Value="{DynamicResource BrushListBoxItemBackgroundSelected}"/>
                                <Setter Property="TextElement.Foreground" TargetName="Border" Value="{DynamicResource BrushListBoxItemForegroundSelected}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True" />
                                    <Condition Property="IsSelected" Value="False" />
                                </MultiTrigger.Conditions>
                                <Setter Property="Panel.Background" TargetName="Border" Value="{DynamicResource BrushListBoxItemBackgroundHighlighted}"/>
                                <Setter Property="TextElement.Foreground" TargetName="Border" Value="{DynamicResource BrushListBoxItemForegroundHighlighted}"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="IndexChildListBoxStyle" TargetType="{x:Type ListBox}">
            <Setter Property="BorderBrush" Value="Transparent" />
            <Setter Property="Margin" Value="5,0,5,0" />
            <Setter Property="IsTextSearchEnabled" Value="True" />
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled" />
            <Setter Property="ItemContainerStyle" Value="{StaticResource ChildItemContainerStyle}" />
        </Style>

        <!-- Parent ListBox -->

        <Style x:Key="IndexItemContainerStyle" TargetType="{x:Type ListBoxItem}">
            <Setter Property="Padding" Value="0" />
            <Setter Property="Margin" Value="0,0,0,10" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Grid>
                            <ContentPresenter x:Name="ContentSite" />
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <DataTemplate x:Key="IndexListBoxDataTemplate">
            <StackPanel>
                <TextBlock Name="Title" FontWeight="Bold" TextTrimming="WordEllipsis" Text="{Binding XPath=@Title}" ToolTip="{Binding XPath=@Title}" Margin="5,0,0,0" Style="{DynamicResource ControlFont}" />
                <ListBox Name="IndexListBox" Style="{StaticResource IndexChildListBoxStyle}" SelectionChanged="OnListBoxSelectionChanged">
                    <ListBox.ItemsSource>
                        <Binding XPath="descendant::*">
                            <Binding.Converter>
                                <CC:FilterForEmbededListBox />
                            </Binding.Converter>
                        </Binding>
                    </ListBox.ItemsSource>
                </ListBox>
            </StackPanel>
        </DataTemplate>

        <Style x:Key="IndexListBoxStyle" TargetType="{x:Type ListBox}">
            <Setter Property="Background" Value="{DynamicResource BrushListBoxBackground}"/>
            <Setter Property="BorderBrush" Value="{DynamicResource BrushListBoxBorder}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="IsTextSearchEnabled" Value="True" />
            <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
            <Setter Property="ScrollViewer.CanContentScroll" Value="False" />
            <Setter Property="Margin" Value="0,5,0,0" />
            <Setter Property="Padding" Value="5" />
            <Setter Property="ItemContainerStyle" Value="{StaticResource IndexItemContainerStyle}" />
            <Setter Property="ItemTemplate" Value="{StaticResource IndexListBoxDataTemplate}" />
        </Style>

        <!-- Style for TreeView -->

        <Style x:Key="IndexTreeViewItemContainerStyle" TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="False"/>
            <Setter Property="Background" Value="Transparent"/>
            <Setter Property="Padding" Value="5,0"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeViewItem}">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition MinWidth="19" Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="21"/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <Controls:SinacorTreeViewToggleButton x:Name="Expander" ClickMode="Press" IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}"/>
                            <Border x:Name="Bd" SnapsToDevicePixels="true" Grid.Column="1" Padding="5,0">
                                <ContentPresenter ContentTemplate="{Binding Path=ItemTemplate, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Controls:SinacorTreeView}}}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" x:Name="PART_Header" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" ContentSource="Header" Style="{DynamicResource ControlFont}" VerticalAlignment="Center"/>
                            </Border>
                            <ItemsPresenter x:Name="ItemsHost" Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" Style="{DynamicResource ControlFont}" VerticalAlignment="Top" Margin="0"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="False">
                                <Setter Property="Visibility" TargetName="ItemsHost" Value="Collapsed"/>
                            </Trigger>
                            <Trigger Property="HasItems" Value="False">
                                <Setter Property="Visibility" TargetName="Expander" Value="Hidden"/>
                            </Trigger>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="CornerRadius" TargetName="Bd" Value="3"/>
                                <Setter Property="Background" Value="{DynamicResource BrushTreeViewItemBackgroundSelected}" TargetName="Bd"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="IsEnabled" Value="False"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" Value="{DynamicResource BrushTreeViewItemBackgroundSelectedUnfocused}" TargetName="Bd"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="True"/>
                                    <Condition Property="IsSelectionActive" Value="False"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" Value="{DynamicResource BrushTreeViewItemBackgroundSelectedUnfocused}" TargetName="Bd"/>
                            </MultiTrigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="TopLevelItemContainerStyle" TargetType="{x:Type TreeViewItem}" BasedOn="{StaticResource IndexTreeViewItemContainerStyle}">
            <Setter Property="IsExpanded" Value="True"/>
            <Setter Property="ItemContainerStyle" Value="{StaticResource IndexTreeViewItemContainerStyle}"/>
        </Style>        

        <HierarchicalDataTemplate x:Key="DataTemplateDefaultTreeView" DataType="NavigationNode">
            <HierarchicalDataTemplate.ItemsSource>
                <Binding XPath="HelpTOCNode">
                    <Binding.Converter>
                        <CC:FilterForTreeView />
                    </Binding.Converter>
                </Binding>
            </HierarchicalDataTemplate.ItemsSource>
            <TextBlock TextTrimming="WordEllipsis" ToolTip="{Binding XPath=@Title}" Text="{Binding XPath=@Title}" />
        </HierarchicalDataTemplate>
        
        <Style x:Key="IndexTreeViewStyle" TargetType="{x:Type TreeView}">
            <Setter Property="ItemTemplate" Value="{StaticResource DataTemplateDefaultTreeView}"/>
            <Setter Property="ItemContainerStyle" Value="{StaticResource TopLevelItemContainerStyle}" />            
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeView}">
                        <Border SnapsToDevicePixels="True" BorderBrush="{DynamicResource BrushTreeViewBorder}" BorderThickness="1" CornerRadius="3" Background="{DynamicResource BrushTreeViewBackground}">
                            <ScrollViewer Focusable="False" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" Background="{DynamicResource BrushTreeViewBackground}" Padding="1" CanContentScroll="false" VerticalScrollBarVisibility="Auto" Margin="1">
                                <ItemsPresenter/>
                            </ScrollViewer>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

    </ContentControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <Controls:SinacorGroupBox Grid.Row="0" Header="{DynamicResource Infra.Controls.Label.Pesquisa}" Margin="0,0,0,0" Width="Auto" Height="75">
            <StackPanel Orientation="Vertical" Margin="2,0,2,2" HorizontalAlignment="Left" VerticalAlignment="Top">
                <StackPanel Orientation="Horizontal">
                    <Controls:SinacorRadioButton Name="toggleTreeViewButton" GroupName="View" Checked="ToggleView" IsChecked="True" Margin="0,0,10,0" Width="80" HorizontalContentAlignment="Left">
                        <Label Width="100" HorizontalContentAlignment="Left" Margin="0,0,0,0" Content="{DynamicResource Infra.Controls.Label.Conteudo}" />
                    </Controls:SinacorRadioButton>
                    <Controls:SinacorRadioButton Name="toggleListBoxButton" GroupName="View" Checked="ToggleView" Width="80" Margin="0,0,15,0" HorizontalContentAlignment="Left">
                        <Label Width="100" HorizontalContentAlignment="Left" Margin="0,0,0,0" Content="{DynamicResource Infra.Controls.Label.Indice}" />
                    </Controls:SinacorRadioButton>
                </StackPanel>
                <Controls:SinacorTextBox Name="FilterTextBox" Grid.Row="1" MaxLines="1" IsUndoEnabled="false" TextChanged="OnFilterTextBoxTextChanged" HorizontalAlignment="Stretch" Width="Auto" Margin="0,0,15,0" />
            </StackPanel>
        </Controls:SinacorGroupBox>

        <Controls:SinacorListBox Name="IndexListBox" Grid.Row="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Style="{StaticResource IndexListBoxStyle}" ItemsSource="{Binding Source={StaticResource NavigationMapListBox}}" IsHitTestVisible="False" Opacity="0" />

        <Controls:SinacorTreeView Name="IndexTreeView" Grid.Row="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Padding="5" Margin="0,5,0,0" 
                                  IsTextSearchEnabled="True" Style="{StaticResource IndexTreeViewStyle}" ItemsSource="{Binding Source={StaticResource NavigationMapTreeView}}" 
                                  SelectedItemChanged="OnTreeViewSelectionChanged" ScrollViewer.HorizontalScrollBarVisibility="Auto" />
    </Grid>
</ContentControl>
